package com.adamjwh.pratice.offer;

import java.util.Stack;

/**
 * 定义栈的数据结构，请在该类型中实现一个能够得到栈中所含最小元素的min函数（时间复杂度应为O（1））。
 */
public class MakeStack {

    private Stack<Integer> stack = new Stack<>();   //数据栈
    private Stack<Integer> minStack = new Stack<>();    //辅助栈

    public void push(int node) {
        if(minStack.empty() || minStack.peek()>=node) { //新元素小于辅助栈栈顶元素时才压入辅助栈
            minStack.push(node);
        } else {    //压入栈顶元素，补全辅助栈，使两栈的元素个数一致
            minStack.push(minStack.peek());
        }
        stack.push(node);
    }

    public void pop() {
        if(stack.empty() || minStack.empty()) {
            return;
        }
        stack.pop();
        minStack.pop();
    }

    public int top() {
        if(! stack.empty()) {
            return stack.peek();
        }
        return 0;
    }

    public int min() {
        if(! minStack.empty()) {
            return minStack.peek();
        }
        return 0;
    }

}
